define(function(require, exports, module){
	var ELView=require("elview");
	var aw = require("ajaxwrapper");
	var Form=require("form");
	var Subnav = require("subnav"); 
	var Dialog=require("dialog");
	var Grid = require("grid");
	var enmu = require("enums");
	var user = require("store").get("user");
	
	var template="<div class='el-dish'>"+
	 "<div class='J-subnav'></div>"+
	 "<div class='J-grid'></div>"+
	 "<div class='J-form hidden'></div>"+
	 "</div>";
	
	var dish = ELView.extend({
		attrs:{
        	template:template
        },
		events:{
			"change .J-form-dish-file-dishimage":function(e){
				var size=e.target.files[0].size;
				var name = e.target.files[0].name;
				var form =this.get("form"); 
				if(Number(size)>10485760){
						Dialog.alert({
							content : "请上传小于10MB的文件",
							confirm : function(){
								Dialog.close();
								form.reset();
							}
						});
					}else{
						form.setValue("imageName",name);
						if($("span[name='dishimage-required']").length>0) {
							$("span[name='dishimage-required']").hide();
						}
				}
			}
		},
		initComponent:function(params,widget){
			var subnav=new Subnav({
        		parentNode:".J-subnav",
				model : {
					title:"菜品档案",
					items : [{
						placeholder:"菜品名称",
						id : "search",
						type : "search",
						handler : function(str) {
							var g = widget.get("grid");
							var subnav=widget.get("subnav");
							var obj={s:str,
									properties:"name",
	            					fetchProperties:"*,servicePoint.*,dishType.*"
	    	        				};
							aw.ajax({
								url:"api/cater/dish/search",
								data:obj,
								dataType:"json",
								success:function(data){
									g.setData(data);
								}
							});
						}
					},{
						id:"dishType",
						type:"buttongroup",
						all:{
							show:true,
							first:true
						},
						tip:"菜品类型",
						lazy : true,
						url:"api/cater/dishtype/query",
						keyField :"pkDishType",
						valueField :"name",
						params: function(){
							return{
								fetchProperties:"pkDishType,name"
							}
						},
						handler:function(key,element){
							widget.get("grid").refresh();
						}
					},{
						id:"state",
						type:"buttongroup",
						all:{
							show:true,
							first:true
						},
						tip:"状态",
						items:enmu["com.eling.elcms.cater.model.Dish.State"],
						params: function(){
							return{
								fetchProperties:"pkDishType,name"
							}
						},
						handler:function(key,element){
							widget.get("grid").refresh();
						}
					},{
						id:"servicePoint",
						type:"buttongroup",
						all:{
							show:false,
							first:true
						},
						tip:"服务点",
						keyField :"pkServicePoint",
						valueField :"name",
						items:user.privateServicePoints,
						params: function(){
							return{
								fetchProperties:"pkServicePoint,name"
							}
						},
						handler:function(key,element){
							widget.get("grid").refresh();
							//联动查询菜品类型
							var form = widget.get("form");
							form.load("dishType");
						}
					},{
						id:"add",
						text:"新增",
						type:"button",
						handler:function(){
							widget.get("form").reset();
							widget.show([".J-form"]).hide([".J-grid"]);
							widget.get("subnav").hide(["add","search","dishType","state"]).show(["return"]);
						}
					},{
						id:"return",
						type:"button",
						text:"返回",
						show : false,
						handler:function(){
							widget.show([".J-grid"]).hide([".J-form"]);
							widget.get("subnav").hide(["return"]).show(["add","search","dishType","state","servicePoint"]);
							widget.get("grid").refresh();
						}
					}],
				}
        	});
        	this.set("subnav",subnav);
        	
        	var grid=new Grid({
        		parentNode:".J-grid",
        		url : "api/cater/dish/query",
        		autoRender : false,
        		params : function() {
    				var subnav=widget.get("subnav");
    				var obj={
    						"dishType":widget.get("subnav").getValue("dishType"),
    						"state":widget.get("subnav").getValue("state"),
    						"servicePoint":widget.get("subnav").getValue("servicePoint"),
    						fetchProperties:"*,servicePoint.*,dishType.*"
	        				};
    				return obj;
    			},
        		model : {
        			columns : [{
						name:"name",
//						className:"name",
						label:"菜品名称"
					},{
						name:"dishType.name",
//        				className:"detial",
        				label:"菜品类型"
					},{
						name:"remark",
//        				className:"detial",
        				label:"备注 "
					},{
						name:"state.value",
//        				className:"detial",
        				label:"状态 "
					},{
						key:"operate",
//						className:"operate",
						name:"操作",
						format:"button",
						formatparams:[{
							key:"stopSale",
							//icon:"icon-edit",
							text:"停售",
							show:function(value,row) {
								if(row.state.key == "Sale") {
									return true;
								} else {
									return false;
								}
							},
							handler:function(index,data,rowEle) {
								//停售逻辑
								aw.ajax({
									url:"api/cater/dish/"+data.pkDish+"/querysetmealdish",
									data:{
		            					fetchProperties:"*"
		    	        				},
									dataType:"json",
									success:function(msg) {
										if(msg.msg == "false") {
											Dialog.confirm({
												title:"提示",
												content:"某菜单有该菜品，是否停售？",
												confirm:function(){
													//是
													aw.ajax({
														url:"api/cater/dish/"+data.pkDish+"/stopsale",
														data:{
							            					fetchProperties:"*"
						    	        				},
														dataType:"json",
														success:function(data) {
															grid.refresh();
														}
													});
												},
											});
										} else {
											aw.ajax({
												url:"api/cater/dish/"+data.pkDish+"/stopsale",
												data:{
					            					fetchProperties:"*"
				    	        				},
												dataType:"json",
												success:function(data) {
													grid.refresh();
												}
											});
										}
									}
								});
							}
						},{
							key:"sale",
							//icon:"icon-edit",
							text:"在售",
							show:function(value,row) {
								if(row.state.key == "Sale") {
									return false;
								} else {
									return true;
								}
							},
							handler:function(index,data,rowEle) {
								//在售逻辑
								aw.ajax({
									url:"api/cater/dish/"+data.pkDish+"/sale",
									data:{
		            					fetchProperties:"*"
	    	        				},
									dataType:"json",
									success:function(data) {
										grid.refresh();
									}
								});
							}
						},{
							key:"edit",
							//icon:"icon-edit",
							text:"编辑",
							show:function(value,row) {
								if(row.state.key == "Sale") {
									return false;
								} else {
									return true;
								}
							},
							handler:function(index,data,rowEle) {
								var grid = widget.get("grid");
								var form = widget.get("form");
								form.reset();
								form.setData(data);
								//图片下载
								$(".J-form-dish-file-dishimage .form-control").val(data.imageName);
								form.download("dishimage","api/attachment/cater/dish/"+data.servicePoint.pkServicePoint+"/"+data.pkDish);
								form.setValue("servicePoint",data.servicePoint.pkServicePoint);
								widget.hide([".J-grid"]).show([".J-form"]);
								widget.get("subnav").hide(["add","search","dishType","state","servicePoint"]).show(["return"]);
								widget.get("grid").refresh();
							}
						},{
							key:"delete",
							//icon:"icon-remove",
							text:"删除",
							show:function(value,row) {
								if(row.state.key == "Sale") {
									return false;
								} else {
									return true;
								}
							},
							handler:function(index,data,rowEle) {
								aw.del("api/cater/dish/"+data.pkDish+"/delete",function() {
									grid.refresh();
								});	
							}
						
						}]
					}]
    			}
    		 });
        	$(".text-center  ").eq(6).attr("style","width:200px;"); 
    		 this.set("grid",grid);
    		 var form=new Form({
         		 parentNode:".J-form",
         		 saveaction : function() {
         			//图片也是必填字段。
         			if($(".J-form-dish-file-dishimage .form-control").val()=="") {
         				if($("span[name='dishimage-required']").length==0) {
         					$(".J-form-dish-file-dishimage .input-group").after('<span for="" name="dishimage-required" '
         						+' style="color:#b94a48;display: block;margin-top: 5px;margin-bottom: 10px;">必填字段</span>');
         				} else {
							$("span[name='dishimage-required']").show();
         				}
         				return false;
         			}
          			var data=widget.get("form").getData();
          			var pkServicePoint = widget.get("subnav").getValue("servicePoint");
          			data.servicePoint = pkServicePoint;
          			aw.saveOrUpdate("api/cater/dish/save",data,function(data) {
          				var form = widget.get("form");
          				//图片上传
          				form.upload("dishimage","api/attachment/cater/dish/"+pkServicePoint+"/"+data.pkDish);
          				widget.hide([".J-form"]).show([".J-grid"]);
     					widget.get("subnav").show(["add","search","dishType","state","servicePoint"]).hide(["return"]);
     					widget.get("grid").refresh();
     				});
     			 },
     			 cancelaction:function(){
     				 	widget.get("grid").refresh();
     				 	widget.show([".J-grid"]).hide([".J-form"]);
     				 	widget.get("subnav").show(["add","search","dishType","state","servicePoint"]).hide(["return"]);
     					return false;
     			 },
	  			 model:{
	 				id:"dish",
	 				layout : '2_1',
					items:[{
							name:"pkDish",
							type:"hidden"
						},{
							name : "version",
							defaultValue : "0",
							type : "hidden"
						},{
							name : "imageName",
							type : "hidden"
						},{
							name:"servicePoint",
							type:"hidden"
						},{
							name:"name",
							label:"菜品名称",
							validate:["required"]
						},{
							name:"dishType",
							label:"菜品类型",
							type : "select",
							keyField : "pkDishType",
							valueField : "name",
							lazy:true,
							validate:["required"],
							url : "api/cater/dishtype/query",
							params: function() {
								var subnav = widget.get("subnav");
								return {
									servicePoint: subnav.getValue("servicePoint"),
									fetchProperties :"pkDishType,name"
								};
							}
						},{
							name:"dishimage",
							label:"图片",
							type:"file",
							maxSize : 51,
							validate:["required"]
						},{
							name:"remark",
							label:"备注",
							type:"textarea",
							"break":true,
							height:100
						}]
			    }
        	 });
        	 this.set("form",form);
		},
		afterInitComponent:function(params,widget){
//        	aw.ajax({
//				url : "api/servicepoint/operableservicepoints",
//				dataType:"json",
//				data : {
//					fetchProperties:"pkServicePoint,name"
//				},
//				success:function(data){
//					widget.get("subnav").setData("servicePoint",data);
					widget.get("grid").refresh();
					//联动查询菜品类型
					widget.get("form").load("dishType");
//				}
//        	});
        	aw.ajax({
				url : "api/cater/dishtype/query",
				dataType:"json",
				data : {
					fetchProperties:"pkDishType,name"
				},
				success:function(data){
					widget.get("subnav").setData("dishType",data);
					widget.get("grid").refresh();
				}
        	});
        }
	});
	module.exports = dish;	
	
});